package com.ls.fw.web.test.todolist.controller;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;

import java.security.Principal;
import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.ExposesResourceFor;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resources;
import org.springframework.hateoas.config.EnableEntityLinks;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ls.fw.web.test.todolist.command.CreateListCommand;
import com.ls.fw.web.test.todolist.command.UpdateListCommand;
import com.ls.fw.web.test.todolist.model.List;
import com.ls.fw.web.test.todolist.model.User;
import com.ls.fw.web.test.todolist.resource.ListResource;
import com.ls.fw.web.test.todolist.resource.ListResourceAssembler;
@EnableEntityLinks
@RestController
@ExposesResourceFor(List.class)
@RequestMapping("/lists")
public class ListRestController {

     @Autowired
     private EntityLinks entityLinks;

    @RequestMapping(method = RequestMethod.GET)
    public Resources<ListResource> readLists(Principal principal) {
        Link link = linkTo(ListRestController.class).withSelfRel();
        return new Resources<ListResource>(new ListResourceAssembler().toResources(new ArrayList<List>()), link);
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public Resources<ListResource> readLists1(Principal principal) {
        Link link = linkTo(ListRestController.class).withSelfRel();
        return new Resources<ListResource>(new ListResourceAssembler().toResources(new ArrayList<List>()), link);
    }

    @RequestMapping(value = "/{listId}", method = RequestMethod.GET)
    public ListResource readList(@PathVariable Long listId) {
        return new ListResourceAssembler().toResource(new List());
    }

    @RequestMapping(value = "/c/{listId}", method = RequestMethod.POST)
    public ResponseEntity<?> createList( ) {
        User user =  new User();
        List list = new List( );
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setLocation(entityLinks.linkForSingleResource(List.class, list).toUri());
        return new ResponseEntity<Object>(responseHeaders, HttpStatus.CREATED);
    }

    @RequestMapping(value = "/{listId}", method = RequestMethod.PUT)
    public ListResource updateList(@PathVariable Long listId, @RequestBody UpdateListCommand updateListCommand) {
        List list = new List();
        list.setName(updateListCommand.getName());
        return new ListResourceAssembler().toResource(list);
    }

    @RequestMapping(value = "/{listId}", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteList(@PathVariable Long listId) {
        return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
    }
}
